package com.ygglxt.servlet;

import com.google.gson.Gson;
import com.ygglxt.entity.YuanGong;
import com.ygglxt.entity.YuanGongSearch;
import com.ygglxt.service.YuanGongService;
import com.ygglxt.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

@WebServlet(name = "YuanGongServlet",urlPatterns = "/YuanGongServlet")
public class YuanGongServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("method");
        request.setCharacterEncoding("utf-8");
        YuanGongService service=new YuanGongService();
        //这里类型要设置未text/json
        response.setContentType("text/json;charset=utf-8");
        if(method.equals("list")){//列表
            String username=request.getParameter("username");
            String sex=request.getParameter("sex");
            String gwid=request.getParameter("gwid");
            String ksdate=request.getParameter("ksdate");
            String jsdate=request.getParameter("jsdate");
            String ksxz=request.getParameter("ksxz");
            String jsxz=request.getParameter("jsxz");
            String pageStr=request.getParameter("page");
            String rowsStr=request.getParameter("rows");
            int page=1;
            int rows=10;
            if(pageStr!=null&&!pageStr.equals("")){
                page=Integer.parseInt(pageStr);
            }
            if(rowsStr!=null&&!rowsStr.equals("")){
                rows=Integer.parseInt(rowsStr);
            }
            YuanGongSearch obj=new YuanGongSearch();
            obj.setUsername(username);
            obj.setSex(sex);
            obj.setGwid(gwid);
            obj.setKsdate(ksdate);
            obj.setJsdate(jsdate);
            obj.setKsxz(ksxz);
            obj.setJsxz(jsxz);
            //获取当前总数
            int dataCount=service.getListCount(obj);
            //获取分页后的列表数据
            List<YuanGong> memberList=service.getListAll((page-1)*rows,rows,obj);
            PrintWriter writer = response.getWriter();
            Map<String,Object> objectMap=new HashMap<>();
            objectMap.put("count",dataCount);
            objectMap.put("data",memberList);
            writer.write(new Gson().toJson(Result.reOk("ok",objectMap)));
        }else if(method.equals("add")){//新增
            //生成唯一ID
            String uuid = String.valueOf(UUID.randomUUID()).replace("-", "");
            String username=request.getParameter("username");
            String age=request.getParameter("age");
            String sex=request.getParameter("sex");
            String rzdate=request.getParameter("rzdate");
            String gwid=request.getParameter("gwid");
            String no=request.getParameter("no");
            String xz=request.getParameter("xz");
            List<Object> objects=new ArrayList<>();
            objects.add(uuid);
            objects.add(username);
            objects.add(age);
            objects.add(sex);
            objects.add(rzdate);
            objects.add(gwid);
            objects.add(no);
            objects.add(xz);
            int count=service.add(objects);
            //将当前用户信息转化为json传回给前端（用的JSON的转化方法）
            PrintWriter writer = response.getWriter();
            if(count==1){//新增成功
                writer.write(new Gson().toJson(Result.reOk("新增成功")));
            }else{//新增失败
                writer.write(new Gson().toJson(Result.reError("新增失败")));
            }
        }else if(method.equals("up")){//修改
            String id=request.getParameter("id");
            String username=request.getParameter("username");
            String age=request.getParameter("age");
            String sex=request.getParameter("sex");
            String rzdate=request.getParameter("rzdate");
            String gwid=request.getParameter("gwid");
            String no=request.getParameter("no");
            String xz=request.getParameter("xz");
            List<Object> objects=new ArrayList<>();
            objects.add(username);
            objects.add(age);
            objects.add(sex);
            objects.add(rzdate);
            objects.add(gwid);
            objects.add(no);
            objects.add(xz);
            objects.add(id);
            int count=service.up(objects);
            //将当前用户信息转化为json传回给前端（用的JSON的转化方法）
            PrintWriter writer = response.getWriter();
            if(count==1){//修改成功
                writer.write(new Gson().toJson(Result.reOk("修改成功")));
            }else{//修改失败
                writer.write(new Gson().toJson(Result.reError("修改失败")));
            }
        }else if(method.equals("get")){//获取当前信息
            String id=request.getParameter("id");
            YuanGong yuanGong=service.getId(id);
            //将当前用户信息转化为json传回给前端（用的JSON的转化方法）
            PrintWriter writer = response.getWriter();
            writer.write(new Gson().toJson(Result.reOk("ok",yuanGong)));
        }else if(method.equals("del")){//删除
            String id=request.getParameter("id");
            List<Object> objects=new ArrayList<>();
            objects.add(id);
            int count=service.del(objects);
            //将当前用户信息转化为json传回给前端（用的JSON的转化方法）
            PrintWriter writer = response.getWriter();
            if(count==1){//删除成功
                writer.write(new Gson().toJson(Result.reOk("删除成功")));
            }else{//删除失败
                writer.write(new Gson().toJson(Result.reError("删除失败")));
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }




}
